/**
 * 
 */
package com.sim.server.config;

import com.sim.core.ImEnvironment;
import com.sim.core.http.HttpConfig;
import com.sim.core.utils.PropUtil;
import com.sim.core.ws.WsConfig;
import org.apache.commons.lang3.StringUtils;

/**
 * @author God
 * 2018/08/26
 */
public class PropertyImServerConfigBuilder extends ImServerConfigBuilder<ImServerConfig,PropertyImServerConfigBuilder> {
	
	public PropertyImServerConfigBuilder(String file) {
		//加载application-{env}.properties
		if(StringUtils.isNotBlank(ImEnvironment.ENV)){
			PropUtil.use(file, ImEnvironment.ENV,null);
		}
	}
	
	@Override
	public PropertyImServerConfigBuilder configHttp(HttpConfig httpConfig)throws Exception{
		httpConfig.setBindPort(PropUtil.getInt("sim.port"));
		//设置web访问路径;html/css/js等的根目录，支持classpath:，也支持绝对路径
		httpConfig.setPageRoot(PropUtil.get("sim.http.page"));
		//不缓存资源;
		httpConfig.setMaxLiveTimeOfStaticRes(PropUtil.getInt("sim.http.max.live.time"));
		//设置s-im mvc扫描目录;
		httpConfig.setScanPackages(PropUtil.get("sim.http.scan.packages").split(","));
		return this;
	}

	@Override
	public PropertyImServerConfigBuilder configWs(WsConfig wsConfig)throws Exception{

		return this;
	}

	@Override
	protected PropertyImServerConfigBuilder getThis() {
		return this;
	}

	@Override
	public ImServerConfig build()throws Exception {
		super.build();
		return ImServerConfig.newBuilder()
				.bindIp(PropUtil.get("sim.bind.ip"))
				.bindPort(PropUtil.getInt("sim.port"))
				.heartbeatTimeout(PropUtil.getLong("sim.heartbeat.timeout"))
				.isStore(PropUtil.get("sim.store"))
				.isSSL(PropUtil.get("sim.ssl"))
				.httConfig(this.httpConfig)
				.wsConfig(this.wsConfig)
				.serverListener(this.serverListener)
				.isCluster(PropUtil.get("sim.cluster")).build();
	}
}
